<?php defined('SYSPATH') OR die('No direct access allowed.');
 
class News_Controller extends Web_Controller {

	// Do not allow to run in production
 
	const ALLOW_PRODUCTION = FALSE;
        public $template = 'kohana/template';

	function index()
	{
		
		$db = new Database;
		$table = 'bz_news';
 
		// In Kohana, all views are loaded and treated as objects.
		
		$news_item = $this->uri->segment('item');
                
		$list = true;
		
		if(!empty($news_item)){

                        $offset = $this->uri->segment('return_page');

			$query = $db->select('*')->from($table)->where('nw_link_local = \''.$news_item."'")->get();
			
			if(count($query[0]) > 0){
				$list = false;
				
				$this->template->content = new View('news_item');
				$this->template->content->item = $query[0];
				
				$this->template->title = $this->template->content->item->nw_title;
			} 
 
		}
		
		if($list){

                        $offset = $this->uri->segment('page');
                        
			$this->template->content = new View('news');
		 
			$this->template->title = 'Новости';

			$page_limit = 30;
			 
			if ($offset < 1) {
				$offset = 1;
			} 

			$query = $db->select('*')->from($table)->orderby('nw_date', 'desc')->orderby('nw_title')->limit($page_limit)->offset(($offset-1)*$page_limit)->get();

			 $this->template->content->items = array();
			 $this->template->content->pagination = '';
			 
			if(count($query)){
				  
				foreach ($query as $item){
				 
					$this->template->content->items[] = $item;
					
				}
				
				if (count($this->template->content->items) > 0) {
					$this->template->content->result = true;
				}   
				
				$pagination = new Pagination(array(
					// Base_url will default to the current URI
					// 'base_url'    => 'welcome/pagination_example/page/x',
				
					// The URI segment (integer) in which the pagination number can be found
					// The URI segment (string) that precedes the pagination number (aka "label")
					'uri_segment'    => 'page',
				
					// You could also use the query string for pagination instead of the URI segments
					// Just set this to the $_GET key that contains the page number
					// 'query_string'   => 'page',
				
					// The total items to paginate through (probably need to use a database COUNT query here)
					'total_items'    => $db->count_records($table),
				
					// The amount of items you want to display per page
					'items_per_page' => $page_limit,
				
					// The pagination style: classic (default), digg, extended or punbb
					// Easily add your own styles to views/pagination and point to the view name here
					'style'          => 'digg',
				
					// If there is only one page, completely hide all pagination elements
					// Pagination->render() will return an empty string
					'auto_hide'      => TRUE,
				));

				$this->template->content->pagination = $pagination->render('digg');
			}
		}

                $this->template->content->page = $offset;

	}
	
	
 
} // End Examples
